package com.tobby.camel.camelinaction.chapter3.order;

import java.util.List;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class PurchaseOrderCsvTest extends CamelTestSupport {

	@Override
	public RouteBuilder createRouteBuilder() {
		context.setTracing(true);
		return new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("file://src/main/java/com/tobby/camel/camelinaction/chapter3/order?noop=true&fileName=order.csv")
						.unmarshal().csv().split(body()).to("mock:queue.csv");
			}
		};
	}

	@Test
	public void testCsvFormat() throws InterruptedException {
		MockEndpoint endpoint = getMockEndpoint("mock:queue.csv");
		endpoint.expectedMessageCount(2);
		assertMockEndpointsSatisfied();

		List line1 = endpoint.getReceivedExchanges().get(0).getIn().getBody(List.class);
		assertEquals("Camel in Action", line1.get(0));
		assertEquals("4995", line1.get(1));
		assertEquals("1", line1.get(2));

		List line2 = endpoint.getReceivedExchanges().get(1).getIn().getBody(List.class);
		assertEquals("Activemq in Action", line2.get(0));
		assertEquals("4495", line2.get(1));
		assertEquals("2", line2.get(2));
	}

}
